package com.tjn.map;

import java.util.*;

/**
 * @author 陶镓妮
 * @version 1.0
 */
public class MapExercise {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        Map hashMap = new HashMap();
        hashMap.put("01",new People("张三",10000,01));
        hashMap.put("02",new People("李四",20000,02));
        hashMap.put("03",new People("王麻子",19000,03));

        //第一种遍历
        Set keyset = hashMap.keySet();
        System.out.println("===第一种遍历===");
        for (Object key : keyset) {
            //不能直接用People调用getSal()方法，要先用get（key）得到value值，此时get(key)的返回类型是Object类型，需要把他强转为People类
            //才能才能调用getSal()方法
            if(((People)hashMap.get(key)).getSal()>18000){
                System.out.println(key + "-" + hashMap.get(key));
            }
        }

        //第二种遍历
        Set entrySet = hashMap.entrySet();
        System.out.println("===第二种遍历===");
        Iterator iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry)iterator.next();//iterator.next()就是Entry，所以直接在这一步强转成Map.ntry
            if(((People)entry.getValue()).getSal()>18000) {
                System.out.println(entry.getKey() + "-" + entry.getValue());
            }
        }

    }
}
class People{
    private String name;
    private double sal;
    private int id;

    public People(String name, double sal, int id) {
        this.name = name;
        this.sal = sal;
        this.id = id;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", id=" + id +
                '}';
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }
}